/**
 * FlightInfoDbTestSuite.java
 * Jan 15, 2010
 */
package com.dlplay.flightinfo.test;

import static org.junit.Assert.*;

import java.io.IOException;


import org.junit.Before;
import org.junit.Test;

import org.restlet.Request;
import org.restlet.Response;
import org.restlet.data.Method;
import org.restlet.data.Reference;
import org.restlet.ext.xml.DomRepresentation;
import org.restlet.routing.Router;
import org.w3c.dom.NodeList;


import com.dlplay.flightinfo.server.FlightInfoApplication;


/**
 * @author zhang yu
 *
 */
public class ResourcesTestSuiteBase extends AbstractGaeTestSuite {
	
	//Constant URLs
	private static final String url = "http://example.com";
	
	private static final String FLIGHT_RESOURCES_URL = url + "/Flight/";
	private static final String FLIGHTS_RESOURCES_URL = url + "/Flights";
	
	private static final String AIRPORTS_RESOURCES_URL = url + "/Airports";
	private static final String AIRPORT_RESOURCES_URL = url + "/Airport/";
	private static final String CLIENT_RESOURCES_URL = url + "/Client/";
	private static final String SYSTEM_RESOURCES_URL = url + "/System/";
	private static final String TICKET_RESOURCES_URL = url + "/Ticket/";
	
	private static final String AIRLINES_RESOURCES_URL = url + "/Airlines";
	private static final String AIRLINE_RESOURCES_URL = url + "/Airline/";
	private static final String AGENCY_RESOURCES_URL = url + "/Agency";
	
	private static final String REGISTER_RESOURCES_URL = url + "/Register";
	
	private static final String PERIOD_RESOURCES_URL = url + "/cron/updateFlight";
	
	
	//Environment
	private Router router = (Router) new FlightInfoApplication().createInboundRoot();;
	private Request request = null;
	private Response response = null;
	private DomRepresentation result = null;
	
	/**
	 * Native method to test the Restlet
	 * @param resourceUrl
	 * @param param
	 * @return
	 * Description:
	 */
	private DomRepresentation getResult(String resourceUrl, String param){
		
		log.info(resourceUrl + param);
		
		if (param != null)
		{
			request =  new Request(Method.GET,
					new Reference(new Reference(url), resourceUrl + param));
		}
		else
		{
			request =  new Request(Method.GET,
					new Reference(new Reference(url), resourceUrl));
		}
		response = new Response(request);
		router.handle(request, response);

		return (DomRepresentation)response.getEntity();
	}
	
	/**
	 * Used to do some preparation before every @test
	 * 
	 * Description:
	 */
	@Before
	public void setUp(){
		log.info("@Before setUp()");
		
		//Reset the enviroment before start a new test.
		request = null;
		response = null;
		result = null;
	}
	
	/**
	 * Used to set up an example for how to add test cases in this test suite
	 * 
	 * Description:
	 */
	@Test
	public void testExample() {
		//TODO add test code here
		log.info("testExample");
		String param = null;
		
		result = getResult(AIRPORTS_RESOURCES_URL, param);
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		log.info("testExample finished");
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testFlight() {
		//TODO Maybe we need a script for 
		log.info("testAirPort");
		String FlightNum = "CA1856";
		
		result = getResult(FLIGHT_RESOURCES_URL, FlightNum);
		try {
			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Number");
			log.info("assertEquals: " + list.item(0).getTextContent());
			assertEquals(list.item(0).getTextContent(), "CA1856");
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testAirPorts() {
		//TODO 
		log.info("testAirPorts");
		String FlightNum = "CA1856";
		
		result = getResult(AIRPORTS_RESOURCES_URL, null);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testAirPort() {
		//TODO 
		log.info("testAirPort");
		String flightNum = "CA1856";
		
		result = getResult(AIRPORT_RESOURCES_URL, null);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testClient() {
		//TODO 
		log.info("testClient");
		String client = null;
		
		result = getResult(CLIENT_RESOURCES_URL, client);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testSystem() {
		//TODO 
		log.info("testSystem");
		String status = null;
		
		result = getResult(SYSTEM_RESOURCES_URL, status);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testTicket() {
		//TODO 
		log.info("testTicket");
		String ticketNum = null;
		
		result = getResult(TICKET_RESOURCES_URL, ticketNum);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testAirLines() {
		//TODO 
		log.info("testTicket");
		String param = null;
		
		result = getResult(AIRLINES_RESOURCES_URL, param);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testAirLine() {
		//TODO 
		log.info("testTicket");
		String param = "airlineNum";
		
		result = getResult(AIRLINE_RESOURCES_URL, param);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * 
	 * 
	 * Description:
	 */
	@Test
	public void testAgency() {
		//TODO 
		log.info("testAgency");
		String param = null;
		
		result = getResult(AGENCY_RESOURCES_URL, param);
//		try {
//			NodeList list = (NodeList) result.getDocument().getElementsByTagName("Airport");
////			assertEquals(list.item(0).getTextContent(), "CA1856");
//			
//		} catch (IOException e1) {
//			// TODO Auto-generated catch block
//			e1.printStackTrace();
//		}
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	public void testRegister() {
		//TODO 
		log.info("testRegister");
		String FlightNum = "CA1856";
		String Email = "zybluerain@gmail";
		
		result = getResult(REGISTER_RESOURCES_URL + FlightNum+Email, null);
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void testPeriodTask() {
		//TODO 
		log.info("testPeriodTask");
		String param = null;
		
		result = getResult(PERIOD_RESOURCES_URL, param);
		
		try {
			log.info("getEntity: " + result.getText());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
